package com.baimawangzi.recovery.dao.impl;

import com.baimawangzi.recovery.bean.Courier;
import com.baimawangzi.recovery.dao.CourierDao;
import com.baimawangzi.recovery.util.dbutil.BeanHandler;
import com.baimawangzi.recovery.util.dbutil.OnlyHandler;

import java.util.List;

public class CourierDaoImpl extends BaseDaoImpl<Courier> implements CourierDao {
    public CourierDaoImpl() {
        clazz= Courier.class;
        init();
    }
    @Override
    public List<Courier> map(String sql, Object[] obj) {
        List<Courier> couriers = queryRunner.query(sql, new BeanHandler<Courier>(clazz), obj);
        return couriers;
    }
    @Override
    public List<Courier> findByLimit(int m, int n) {
        String sql="select *from t_courier limit ?,?";
        Object[] object= {m, n};
        List<Courier> couriers = queryRunner.query(sql,new BeanHandler<>(Courier.class),object);
        return couriers;
    }

    @Override
    public long getCount() {
        String sql="select count(*) totalSize from t_courier where ?=?";
        Object[] object= {1, 1};
        List<Object> onlylist = queryRunner.query(sql,new OnlyHandler(),object);
        return (long) onlylist.get(0);
    }

    @Override
    public int add(Courier courier) {
        String sql="insert into t_courier values (null,?,?,?)";
        //insert into t_courier values (null,'1111111111111','空闲中',12565777865);
        Object[] obj={courier.getCouriername(),courier.getStatus(),courier.getPhonenum()};
        return queryRunner.update(sql,obj);

    }

    @Override
    public int delete(int id) {
        String sql ="delete from t_courier where id=?";
        Object[] obj={id};
        return queryRunner.update(sql,obj);
    }

    @Override
    public Courier findById(int id) {
        String sql="select *from t_courier where id = ?";
        Object[] object= {id};
        Courier courier = queryRunner.query(sql, new BeanHandler<>(Courier.class), object).get(0);

        return courier;
    }

    @Override
    public int update(Courier courier) {
        String sql="update t_courier set couriername=?,status=?,phonenum=? where id=?;";
        Object[] object= {courier.getCouriername(),courier.getStatus(),courier.getPhonenum(),courier.getId()};
        return queryRunner.update(sql,object);
    }

}
